import 'package:flutter/cupertino.dart';
import 'package:flutter/widgets.dart';
import 'package:hrf_app/common/api/http_head.dart';
import 'package:hrf_app/common/models/material/material.dart';

import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';
import 'index.dart';

class MaterialSearchPage extends GetView<MaterialSearchPageListController> {
  const MaterialSearchPage({super.key});

  Widget _buildView() {
    return Column(
      children: [
        _searchInput(),
        Expanded(
          child: _buildViewList(),
        )
      ],
    );
  }

  Widget _buildViewList() {
    List<MaterialModel> materialList = controller.materialList;
    return Column(
      children: [
        Expanded(
            child: EasyRefresh(
          header: const ClassicHeader(
            dragText: '下拉刷新',
            armedText: '正在刷新...',
            processedText: '刷新完成',
            failedText: '刷新失败',
            noMoreText: '没有更多数据',
            showText: true,
            showMessage: true,
            messageText: '上次更新于 %T', // 默认时间格式
          ),
          footer: const ClassicFooter(
            dragText: '上拉加载更多',
            armedText: '释放加载更多',
            readyText: '准备加载更多',
            processingText: '正在加载...',
            processedText: '加载完成',
            failedText: '加载失败',
            noMoreText: '没有更多数据',
            showText: true,
            showMessage: true,
            messageText: '上次更新于 %T',
          ),
          controller: controller.easyRefreshController,
          onRefresh: () async {
            print("下拉刷新逻辑");
            controller.onRefresh();
          },
          onLoad: () async {
            print("上拉加载逻辑");
            controller.onLoad();
          },
          child: ListView.builder(
              itemCount: materialList.length,
              itemBuilder: (context, index) {
                MaterialModel materialModel = materialList[index];
                return _buildItemView(materialModel);
              }),
        ))
      ],
    );
  }

  Widget _searchInput() {
    return TDSearchBar(
      placeHolder: "搜索",
      style: TDSearchStyle.round,
      onTextChanged: (value) {
        controller.searchName(value);
      },
    );
  }

  // 主视图
  Widget _buildItemView(MaterialModel materialModel) {
    return Container(
        margin: const EdgeInsets.all(8),
        padding: const EdgeInsets.all(8),
        decoration: BoxDecoration(
          color: Colors.white,
          boxShadow: const [BoxShadow(color: Colors.grey, blurRadius: 0.3)],
          borderRadius: BorderRadius.circular(10),
        ),
        child: SizedBox(height: 84, child: _buildMaterialInfo(materialModel)));
  }

  Widget _buildMaterialInfo(MaterialModel materialModel) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        Expanded(
          child: _buildMaterialImage(materialModel),
          flex: 1,
        ),
        SizedBox(
          width: 16,
        ),
        Expanded(
          child: _buildBugInfo(materialModel),
          flex: 3,
        )
      ],
    );
  }

  Widget _buildMaterialImage(MaterialModel materialModel) {
    return TDImage(
      width: 80,
      height: 80,
      imgUrl: materialModel.homeImageUrl == null
          ? ""
          : HttpHead.minioUrl() + materialModel.homeImageUrl!,
    );
  }

  Widget _buildBugInfo(MaterialModel materialModel) {
    List<Widget> columList = [];
    columList.add(buildInfoDetailTitle("名称：", materialModel.name!));
    columList.add(buildInfoDetailTitle("编码：", materialModel.code ?? ""));
    columList.add(buildInfoDetailTitle("备注:", materialModel.remark ?? ""));
    return InkWell(
        onTap: () {
          controller.onJumpToDetail(materialModel.id);
        },
        child: Column(children: columList));
  }

//标题内容行
  Widget buildInfoDetailTitle(String title, String content) {
    return Row(
      children: [
        TDText(
          title,
          fontWeight: FontWeight.w700,
        ),
        Expanded(
          child: TDText(
            content,
            overflow: TextOverflow.ellipsis,
          ),
        )
      ],
    );
  }

//标题

  Widget buildInfoDetailIDLeftRight(Widget leftWidget, Widget rightWidget) {
    return Row(
      children: [
        Expanded(flex: 3, child: leftWidget),
        Expanded(flex: 3, child: rightWidget),
      ],
    );
  }

  Widget itemTitle(title) {
    return TDText(
      title,
      fontWeight: FontWeight.w700,
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<MaterialSearchPageListController>(
      init: MaterialSearchPageListController(),
      id: "materialSearchPage",
      builder: (_) {
        return Scaffold(
          appBar: AppBar(
            title: const Text(
              "配件查询",
            ),
          ),
          floatingActionButton: FloatingActionButton(
            onPressed: () {
              controller.jumpToProjectButAdd();
            },
            tooltip: '增加',
            shape: CircleBorder(),
            child: Icon(Icons.add),
          ),
          body: SafeArea(
            child: _buildView(),
          ),
        );
      },
    );
  }
}
